<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请假信息管理系统 -- 添加学院</title>
    <script src="../../../js/jquery.min.js"></script>
    <link href="../../../css/departmentManage/addDepartment.css" rel="stylesheet">
    <link href="../../../css/common/common.css" rel="stylesheet">
</head>
<body>
<!-- 顶部弹出提示信息容器 -->
<div id="notification" class="notification">
    <span id="message"></span>
</div>
    <!-- 头部 -->
    <header th:insert="common/common::header">
    </header>
    <!-- 左边 -->
    <aside th:insert="common/common::aside">
    </aside>
    <!-- 中间 -->
    <main class="main">
        <form action="/department/addDepartment" id="depFrom">
            <h2 style="margin: 20px 30%;">新增二级学院信息</h2>
            <div class="form-group">
                <label for="depId">二级学院编号</label>
                <input type="text" id="depId" name="depID">
            </div>
            <div class="form-group">
                <label for="depName">二级学院名称</label>
                <input type="text" id="depName" name="depName">
            </div>
            <input type="button" value="返回" onclick="goBack()">
            <input type="button" value="保存提交" onclick="addDepartment()">
        </form>

    </main>
</body>

<script th:inline="javascript" type="text/javascript">
    function goBack(){
        window.location.href = '/department/list?pageSize=7&pageNum=1'
    }
    var ids = [[${depIds}]];
    console.log(ids)
    function addDepartment(){
        var depId = document.getElementById("depId").value;
        var depName = document.getElementById("depName").value;
        if(depId.length == 0 || depId.length >= 30){
            document.getElementById("message").innerHTML = "学院编号非法，请重新输入";
            this.showNotification();
            return false;
        }
        if(depName.length == 0 || depName.length >= 50){
            document.getElementById("message").innerHTML = "学院名称非法，请重新输入";
            this.showNotification();
            return false;
        }
        for(var id in ids){
            console.log(id)
            console.log(depId)
            if(depId === ids[id]){
                document.getElementById("message").innerHTML = "编号已存在，请重新输入";
                this.showNotification();
                return false;
            }
        }
        document.getElementById("depFrom").submit();
    }

    function showNotification() {
        const notification = document.getElementById("notification");
        notification.classList.add("show");

        setTimeout(function() {
            notification.classList.remove("show");
        }, 2000); // 3秒后自动消失
    }

    function outLogin(){
        location.href = '/outLogin'
    }
</script>

</html>